﻿using Flurl.Http;
using Pay.Core;
using Pay.DTO.h5;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Pay.Service.Pay
{
    public class H5Sign
    {
        private string publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvwPvo+HpePqlRcBBR9JD9aXmy0frrz8R8E3msXO24eapUxuRbaN507lCDyXGSK75+cJIvct050ei0oeNGl3mGygrQv5ReyRr9jl81xX53Q9YALcCkjyNNZWFQoYaBgl/aEgeNZSOfFZ1QtNbHg6kR8YiITE6Xf+VAS50dNdI8KsfLEYc1aq0+KuRur191DzPQ3Y528pT1BBGj3qk13mGwWEIY3/jgHT0PnxnXSgMXLGpbRFZr2ldxQ/f0iF666oL1EeeBSbUqyoPlbfK2owVOSN9Dwuy3EfiwaZy7OHxY3e6pENqL2bZVtIXE9+JR2HlS7JnJRt4iTwscwSfnEJlaQIDAQAB";
        private string privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDS6GbZwe1sBfHGh2bgCaneI/OLIgX333e/uLkHNqibNg3IunSLcMS8+fLfrbO2pX4MD9TowP7w/271KefWLL8OF0E/wyHCjTrpIrPwyVvIeQyTYjyiNLaS0esRtKhlLqnVnbuggjdUDf2m5dtUGiOfgZ8yE/cRva5Je5Qglpfn9VFlMwCeN8gA5MDA+ojBWnhhaU8ZrPnVJDfOREdIE0vQKWLWl/8ORtd3du5yuqE0lF8SNeX68aKlQcDjfByxhbgQy9zuHVTiqI2c0VQV483HytsazNPgHEF9ilXLTLHMMfYiwZrOsc9E7ivS+e76FHlf3XszaWEKn25nbJcR+RsBAgMBAAECggEBALJyRCNWe6V8WhaZladVRc5YKMW9ocMZ5l3m01aRHpNEZuFBMT6RyUQDT0WF2Jpe54kZFBFiydHqrVzteeSys2werOxgE35itEdxP0IpfGjgUo2Y/qhXZWpQBk5Sxm/lzXiaHpgt1NZfiA/gR3S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
        private string tokenReqUrl = "http://idp.finopen.cn:8080/oauth/token";
        private string payUrl = "http://test.mypays.cn/api/industry/get-all";

        public async Task<h5_token_res> GetAuthToken(h5_token_req model)
        {
            var log = IocHelper.Get<ILogHelper>("info");
            var json = model.ToJson();
            log.Info(json);

            try
            {
                //注意：本地浏览器开启代理模式，请求会指向代理ip。
                var content = json.ToObject<Dictionary<string, string>>();
                var formUrlContent = new FormUrlEncodedContent(content);
                //var resp = await tokenReqUrl
                //    .WithHeader("Content-Type", "application/x-www-form-urlencoded")
                //    .PostAsync(formUrlContent).Result.Content.ReadAsStringAsync();
                var resp = await tokenReqUrl.PostAsync(formUrlContent).ReceiveJson<h5_token_res>();
                log.Info(resp.ToJson());
                return resp;
            }
            catch (FlurlHttpException ex)
            {
                log.Error(ex.Message);
                return null;
            }
        }

        public async Task<pay_result> GetPay(string token)
        {
            var log = IocHelper.Get<ILogHelper>("info");

            try
            {
                string parameters = "{}";
                string sign = RSAFromPkcs.sign(parameters, privateKey, "utf-8");
                var resp = await payUrl.
                        WithHeader("Authorization", "Bearer " + token).
                        WithHeader("x-token-sign", sign).
                        PostJsonAsync(parameters).ReceiveJson<pay_result>();
                log.Info(resp.ToJson());
                return resp;
            }
            catch (FlurlHttpException ex)
            {
                log.Error(ex.Message);
                return null;
            }
        }
    }
}
